import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
import java.util.StringTokenizer;

//Answer with a proper noun or other entity

public class WhoDid extends Who {

	public WhoDid(String question, int num) {
		this.num = num;
		this.question = question;

		words = StopWordRemover.remove(question);
		Iterator<String> iter = words.iterator();
		while (iter.hasNext()) {
			keyWords = keyWords + iter.next() + " ";
		}
		
		parseBing();
		parseWikipedia();
		
		int window = 20;
		String[] text = new String[3];
		String[] unstemmedText = new String[3];
		for (int i = 0; i < text.length; i++) {
			text[i] = data[i].toString();
			unstemmedText[i] = unstemmed[i].toString();
		}
		
		for (int n = 0; n < text.length; n++) {
			try {
				title[n] = title[n].replace(",", "");
				title[n] = title[n].replace(".", "");
			}
			catch (Exception e) {
				return;
			}
			candidateAnswers.add(title[n]);
			StringTokenizer st = new StringTokenizer(text[n]);
			String[] list = new String[st.countTokens()];
			StringTokenizer st2 = new StringTokenizer(unstemmedText[n]);
			String[] unstemmedList = new String[st2.countTokens()];
			for (int i = 0; i < list.length; i++) {
				list[i] = st.nextToken();
				unstemmedList[i] = st2.nextToken();
			}

			String[] titleWords = title[n].split(" ");
			
			Object[] tokens = words.toArray();
			ArrayList<String> tokenList = new ArrayList<String>();
			for (int i = 0; i < tokens.length; i++) {
				tokenList.add(((String) tokens[i]).toLowerCase());
			}
			for (int i = 0; i < titleWords.length; i++) {
				titleWords[i] = titleWords[i].toLowerCase();
				if (tokenList.contains(titleWords[i])) tokenList.remove(tokenList.indexOf(titleWords[i]));
			}

			for (int i = 0; i < tokenList.size(); i++) {
				Stemmer stem = new Stemmer();
				stem.add(tokenList.get(i).toCharArray(), tokenList.get(i).length());
				stem.stem();
				tokenList.set(i, stem.toString());
			}
			
			
			boolean[] seen = new boolean[tokenList.size()];

			int start = 0;
			int end = 0;
			
			/*
			try {
				FileWriter fstream = new FileWriter("corpus.txt");
				BufferedWriter out = new BufferedWriter(fstream);
				for (int i = 0; i < list.length; i++) {
					out.write(list[i] + " ");
				}
				out.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			*/

			String data = "";

			for (int t = 0; t < list.length; t++) {

				data = "";
				String token = list[t];
				if (tokenList.contains(token.toLowerCase())) {
					data = data + token + " ";
					start = t;
					end = start;
					for (int i = 0; i < tokenList.size(); i++) {
						if ((tokenList.get(i)).equals(token)) {
							seen[i] = true;
						}
					}
					for (int i = 1; i < window; i++) {
						if (i + t < list.length)
							token = (String) list[i + t];
						data = data + token + " ";
						for (int j = 0; j < tokenList.size(); j++) {
							if ((tokenList.get(j)).equals(token)) {
								seen[j] = true;
								end = i + t;
							}
						}
					}

					boolean allfound = true;
					if (seen[0] == true) {
						for (int i = 0; i < seen.length; i++) {
							if (!seen[i])
								allfound = false;
						}
					} else
						allfound = false;

					seen = new boolean[tokenList.size()];

					if (allfound) {

						String phrase = "";
						for (int i = start - 10; i < end + 10; i++) {
							if (i < 0)
								i = 0;
							if (i >= list.length)
								break;
								phrase = phrase + unstemmedList[i] + " ";
						}
						candidatePhrases.add(phrase);
					}
				}

			}
		}
		
		//End of o(scary) for loops
		
		for (int i = 0; i < candidatePhrases.size(); i++) {
			String phrase = QA.tagger.tagString(candidatePhrases.get(i));
			StringTokenizer st = new StringTokenizer(phrase);
			while (st.hasMoreTokens()) {
				String token = st.nextToken();
				if (token.contains("NNP")) {
					String answer = token.replace("/NNP", "");
					while (st.hasMoreTokens() && (token = st.nextToken()).contains("NNP")) {
						answer = answer + " " + token.replace("/NNP", "");
					}
					if (!candidateAnswers.contains(answer)) candidateAnswers.add(answer);
				}
			}
		}
		
		//Clean the candidate answers.
		int len = candidateAnswers.size();
		for (int i = 0; i < len; i++) {
			if (candidateAnswers.get(i).charAt(candidateAnswers.get(i).length() - 1) == 'S') {
				candidateAnswers.set(i, candidateAnswers.get(i).substring(0, candidateAnswers.get(i).length() - 1));
			}
			System.out.println(candidateAnswers.get(i));
		}
		
/*
		for (int i = 0; i < candidateAnswers.size(); i++) {
			try {
				String url = "http://www.bing.com/search?q="
						+ candidateAnswers.get(i) + "+"
						+ keyWords.replace(" ", "+")
						+ "%3F&go=&form=QBRE&qs=n&sk=&sc=8-18";

				URL wiki = new URL(url);
				URLConnection wc = wiki.openConnection();
				InputStreamReader in = new InputStreamReader(
						wc.getInputStream());
				BufferedReader br = new BufferedReader(in);
				String inputLine;

				StringBuilder sb = new StringBuilder();

				while ((inputLine = br.readLine()) != null) {
					sb.append(inputLine);
				}

				String result = sb.toString();
				in.close();

				int startindex = result.indexOf("1-10 of ")
						+ "1-10 of ".length();
				int endindex = result.indexOf(" results", startindex);
				if (startindex < 0 || endindex < 0)
					continue;
				String numResults = result.substring(startindex, endindex);
				System.out.println(candidateAnswers.get(i) + " " + numResults);

			} catch (Exception e) {
				i--;
				continue;
			}
		}
 		*/
		
		searchCorpus();
		

	}

	public void searchCorpus() {
		//Search through the documents provided and look for:
			//Candidate answers plus any number of the key words in  the surrounding window
		    //Pick out the answer that maximizes the number of key words surrounding the candidate answer, the most number of times.
		    //Then, pick out the 5 documents that are closest to our top 5 answers.
		
	}

	
	//public String toString() {
	//	return question + " " + keyWords;
	//}
	
	public void parseWikipedia() {
		try {
			//http://en.wikipedia.org/w/index.php?title=Special%3ASearch&redirs=1&search=hello+goodbye&fulltext=Search&ns0=1
			String url = "http://en.wikipedia.org/w/index.php?title=Special%3ASearch&redirs=1&search=";
			url = url + keyWords.replaceAll(" ", "+");
			url = url + "&fulltext=Search&ns0=1";
			URL wiki = new URL(url);
			URLConnection wc = wiki.openConnection();
			InputStreamReader in = new InputStreamReader(wc.getInputStream());
			BufferedReader br = new BufferedReader(in);
			String inputLine;
	
			StringBuilder sb = new StringBuilder();
			
			while ((inputLine = br.readLine()) != null) {
				sb.append(inputLine);
			}
			
			String text = sb.toString();
			in.close();
			
			String[] pages = new String[3];
			data = new StringBuilder[pages.length];
			unstemmed = new StringBuilder[pages.length];
			for (int i = 0; i < data.length; i++) {
				data[i] = new StringBuilder();
				unstemmed[i] = new StringBuilder();
			}
			int index = 0;
			title = new String[pages.length];
			for (int i = 0; i < pages.length; i++) {
				index = text.indexOf("mw-search-result-heading", index + 1);
				int startindex = text.indexOf("\"", index) + 1;
				int endindex = text.indexOf("\"", startindex + 1);
				String link = text.substring(startindex, endindex);
				pages[i] = "http://en.wikipedia.org" + link;
			
				URL page = new URL(pages[i]);
				URLConnection pc = page.openConnection();
				InputStreamReader pci = new InputStreamReader(pc.getInputStream());
				BufferedReader pcb = new BufferedReader(pci);
				
				StringBuilder sb2 = new StringBuilder();
				
				while ((inputLine = pcb.readLine()) != null) {
					sb2.append(inputLine);
				}
				
				String wikipage = sb2.toString();
				sb2 = new StringBuilder();
				
				int starttitle = wikipage.indexOf("<title>") + "<title>".length();
				int endtitle = wikipage.indexOf(" - Wiki");
				title[i] = wikipage.substring(starttitle, endtitle);
				
				int pageindex = 0;
				while (true) {
					int lastindex = pageindex;
					pageindex = wikipage.indexOf("<p>", pageindex);
					if (lastindex > pageindex) break;
					int pagestartindex = pageindex + 3;
					int pageendindex = wikipage.indexOf("</p>",pagestartindex);
					String paragraph = wikipage.substring(pagestartindex, pageendindex);
					paragraph = paragraph.toString().replaceAll("\\<.*?>","");
					paragraph = paragraph.replace(".", "");
					paragraph = paragraph.replace(",", "");
					//paragraph = paragraph.replace("'", "");
					//paragraph = paragraph.replace("\"", "");
					StringTokenizer st = new StringTokenizer(paragraph);
					while (st.hasMoreTokens()) {
						Stemmer stem = new Stemmer();
						String partoken = st.nextToken();
						stem.add(partoken.toCharArray(), partoken.length());
						stem.stem();
						data[i] = data[i].append(stem.toString() + " ");
						unstemmed[i] = unstemmed[i].append(partoken + " ");
					}
					//unstemmed[i] = unstemmed[i].append(paragraph + " ");
					pageindex = pageendindex;
				}
			}			
		}
		catch (Exception e) {
			
		}

	}
	
	public void parseBing() {
		String url = "http://www.bing.com/search?q=" 
			+ question.replace(" ", "+") +
			"%3F&go=&form=QBRE&qs=n&sk=&sc=8-18";
		System.out.println(url);
		try {
			URL wiki = new URL(url);
			URLConnection wc = wiki.openConnection();
			InputStreamReader in = new InputStreamReader(wc.getInputStream());
			BufferedReader br = new BufferedReader(in);
			String inputLine;
	
			StringBuilder sb = new StringBuilder();
			
			while ((inputLine = br.readLine()) != null) {
				sb.append(inputLine);
			}
			
			String text = sb.toString();
			
			int endindex = 0;
			int startindex = text.indexOf("<cite>", endindex) + "<cite>".length();
			endindex = text.indexOf("</cite>", startindex);
			url = text.substring(startindex, endindex);
			url = url.replaceAll("\\<.*?>","");
			
			System.out.println(url);
			
			if (!url.contains("http")) url = "http://" + url;
			URL page = new URL(url);
			URLConnection pc = page.openConnection();
			InputStreamReader pci = new InputStreamReader(pc.getInputStream());
			BufferedReader pcb = new BufferedReader(pci);
			
			StringBuilder sb2 = new StringBuilder();
			
			while ((inputLine = pcb.readLine()) != null) {
				sb2.append(inputLine);
			}
			
			pci.close();
			
			String wikipage = sb2.toString();
			sb2 = new StringBuilder();
			
			wikipage = wikipage.replaceAll("\\<.*?>","");

			String taggedBing = QA.tagger.tagString(wikipage);
			

			StringTokenizer st = new StringTokenizer(taggedBing);
			while (st.hasMoreTokens()) {
				String token = st.nextToken();
				if (token.contains("NNP")) {
					String answer = token.replace("/NNP", "");
					while (st.hasMoreTokens()
							&& (token = st.nextToken()).contains("NNP")) {
						answer = answer + " " + token.replace("/NNP", "");
					}
					if (!candidateAnswers.contains(answer))
						candidateAnswers.add(answer);
				}
			}
			

		}
		catch (Exception e) {

		}
	}

}
